package com.lcq.springbootcreate.project.dao;

import com.lcq.springbootcreate.project.entity.Category;
import com.lcq.springbootcreate.project.request.AddCategoryReq;
import com.lcq.springbootcreate.project.request.UpdateCategoryReq;
import com.lcq.springbootcreate.project.vo.CategoryVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface CategoryDAO {

    @Select("select * from imooc_mall_category where name = #{addCategoryReq.name}")
    Category selectByName(@Param("addCategoryReq") AddCategoryReq addCategoryReq);

    @Insert("inert into imooc_mall_category (name,type,parent_id,order_name) values(#{addCategoryReq.name},#{addCategoryReq.type},#{addCategoryReq.parent_id},#{addCategoryReq.order_name})")
    int insertByCategoryReq(@Param("addCategoryReq") AddCategoryReq addCategoryReq);

    @Select("select * from imooc_mall_category where name = #{name}")
    Category selectByCategoryName(@Param("name") String name);

    @Update("<script>" +
            "update imooc_mall_category " +
            "<set>" +
            "<if test = \"c.name != null and c.name != '' \">" +
            "name = #{c.name}" +
            "</if>" +
            "<if test = \"c.type != null and c.type != '' \">" +
            "type = #{c.type}" +
            "</if>" +
            "</set>" +
            "where id = #{c.id}" +
            "</script>")
    int updateByCategory(@Param("c") UpdateCategoryReq c);

    @Delete("delete from imooc_mall_category where id = #{id}")
    int deleteById(@Param("id") Integer id);

    @Select("select * from imooc_mall_category")
    List<CategoryVO> getListAll();

    @Select("select * from imooc_mall_category where parent_id = #{parentId}")
    List<Category> getListByParentId(@Param("parentId") Integer parentId);

}

